

package top.infopub.mgr.bpa.web;


import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.infopub.enums.mkt.ProProcesssStatusEnum;
import top.infopub.mgr.bpa.domain.InnerAgrtDomain;
import top.infopub.mgr.bpa.domain.InnerLedgerDomain;
import top.infopub.mgr.bpa.jsonVo.InnerAgrtJsonVo;
import top.infopub.mgr.bpa.service.*;
import top.infopub.mgr.bpa.vo.ContractManageVo;
import top.infopub.mgr.bpa.vo.InnerAgrtVo;
import top.infopub.mgr.common.page.ParamPage;
import top.infopub.mgr.common.vo.RspVo;
import top.infopub.mgr.flow.web.GenericFlowController;
import top.infopub.mgr.hr.service.OrgInfoService;
import top.infopub.mgr.hr.service.StaffInfoService;
import top.infopub.mgr.mkt.service.MarketManageService;
import top.infopub.mgr.mkt.service.ProProcessManageService;
import top.infopub.mgr.sg.service.CoopInfoService;
import top.infopub.model.bpa.ContractEntry;
import top.infopub.model.bpa.InnerAgrt;
import top.infopub.model.hr.OrgInfo;
import top.infopub.model.hr.StaffInfo;
import top.infopub.model.mkt.ProjectAttachment;
import top.infopub.security.util.SessionUserUtils;
import top.infopub.security.vo.AuthorizationVO;
import top.infopub.tdp.sys.service.CodeGeneratorConfigService;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**对内合同
 * @author wuzhiyang
 * @version 2020年2月14日
 * @see InnerAgrtController
 * @since商务管理/内部协议/评审
 */
@Controller
@RequestMapping("/innerAgrt")
public class InnerAgrtController extends GenericFlowController {

    @Autowired
    private InnerAgrtService innerAgrtService;

    @Autowired
    private MarketManageService marketManageService;

    @Autowired
    private ProProcessManageService proProcessManageService;

    @Autowired
    private BpaProcessManageService bpaProcessManageService;
    /**
     * 内部协议列表
     * @return
     * @see
     */
    @RequestMapping("/list")
    public String list(HttpServletRequest request) {
        //获取登录人员信息
        AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
        //查询区域公司列表
        List<OrgInfo> orgInfoList = marketManageService.queryOrgInfoByAuthUserId(vo);
        request.setAttribute("orgInfoList", orgInfoList);
        //查询营销人员列表
        List<StaffInfo> staffInfoList = marketManageService.queryStaffInfoByAuthUserId(vo);
        request.setAttribute("staffInfoList", staffInfoList);
        return "bpa/lay-innerAgrt-list";
    }

    /**
     * 内部协议列表数据
     * @return
     */
    @RequestMapping("/queryDataByCondition")
    @ResponseBody
    public RspVo<InnerAgrtDomain> queryDataByCondition(InnerAgrtVo innerAgrtVo, ParamPage paramPage) throws ParseException {
        Page<InnerAgrtDomain> page = new Page<InnerAgrtDomain>();
        page.setPageNum(paramPage.getPage());
        page.setPageSize(paramPage.getLimit());
        //查询时，结束日期增加一天
        if (innerAgrtVo.getEndDate() != null) {
            innerAgrtVo.setEndDate(DateUtils.addDays(innerAgrtVo.getEndDate(), 1));
        }
        page = innerAgrtService.selectByDomainWhere(page, innerAgrtVo);
        return RspVo.getSuccessResponse(page.getTotal(), page.getResult());
    }

    /**
     * 分包合同台账
     *
     * @return
     */
    @RequestMapping("/selectByLedger")
    @ResponseBody
    public RspVo<InnerLedgerDomain> selectByLedger(ContractManageVo contractManageVo, ParamPage paramPage) throws ParseException {
        Page<InnerLedgerDomain> page = new Page<InnerLedgerDomain>();
        page.setPageNum(paramPage.getPage());
        page.setPageSize(paramPage.getLimit());
        //查询时，结束日期增加一天
        if (contractManageVo.getEndDate() != null) {
            contractManageVo.setEndDate(top.infopub.mgr.util.DateUtils.addDays(contractManageVo.getEndDate(), 1));
        }
        page = innerAgrtService.selectByLedger(page, contractManageVo);
        return RspVo.getSuccessResponse(page.getTotal(), page.getResult());
    }

    /**
    *@Description: 内部协议-合同评审 筛选项目- 承继协议中未发起过内部协议的项目
    *@Param:
    *@return:
    *@Author: Tszzzzzzz
    *@date: 2021/6/17
    */
    @RequestMapping("/queryProject")
    @ResponseBody
    public RspVo queryProject(){
       return innerAgrtService.selectProject();
    }


    /**
    *@Description: 查询详情
    *@Param:
    *@return:
    *@Author: Tszzzzzzz
    *@date: 2021/6/17
    */
    @RequestMapping("/queryById")
    @ResponseBody
    public RspVo queryById(String id){
     return innerAgrtService.queryById(id);
    }


    /**
    *@Description: 内部协议-合同评审 保存
    *@Param:
    *@return:
    *@Author: Tszzzzzzz
    *@date: 2021/6/17
    */
    @RequestMapping("/save")
    @ResponseBody
    public RspVo save(String bodyStr,Integer submitType){
        AuthorizationVO userDtl = SessionUserUtils.getSessionAttributeForUserDtl();
        String s = StringEscapeUtils.unescapeJava(bodyStr);
        InnerAgrtJsonVo vo = JSON.parseObject(s,InnerAgrtJsonVo.class);
        InnerAgrt innerAgrt = new InnerAgrt();
        if (StringUtils.isNotBlank(vo.getInnerAgrt().getId())){ //编辑
            BeanUtils.copyProperties(vo.getInnerAgrt(),innerAgrt);
            innerAgrt.setUptBy(userDtl.getId());
            innerAgrt.setUptTime(new Date());
        }else{//新增
            BeanUtils.copyProperties(vo.getInnerAgrt(),innerAgrt);
            innerAgrt.setAddBy(userDtl.getId());
            innerAgrt.setAddTime(new Date());
        }
        /**
         * 附件处理
         * */
        List<ProjectAttachment> attachmentList = new ArrayList<>();
        if(vo.getFileAtt()!=null){
            String[] fileAtt = vo.getFileAtt();
            for (String file : fileAtt) {
                ProjectAttachment attachment = createProjectFileAtta(userDtl,innerAgrt.getProCode(),file);
                attachmentList.add(attachment);
            }
        }
        return innerAgrtService.saveAll(submitType,userDtl,innerAgrt,vo.getInnerListings(),vo.getInnerAgrtSup(),vo.getCompPlan(),attachmentList,bodyStr);
    }

    @RequestMapping("/toDealOrDetail")
    public String queryById(HttpServletRequest request, String id) {
        AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
        request.setAttribute("id",id);
        InnerAgrt innerAgrt = innerAgrtService.selectBySguid(id);
        request.setAttribute("isAuditFlag", "1");
        if (innerAgrt!=null){
            if (StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS1.getCode(), innerAgrt.getProcessStatus())) {
                request.setAttribute("isAuditFlag", "0");
            }
            if (StringUtils.isNotBlank(innerAgrt.getProcessId()) && innerAgrt.getProcessId().length() > 10){
                request.setAttribute("processId", innerAgrt.getProcessId());
                String auditTabFlag = proProcessManageService.checkShowAuditTab(innerAgrt.getProcessId(), vo);
                if (StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS1.getCode(), innerAgrt.getProcessStatus())
                        && StringUtils.equals("0", auditTabFlag)) {
                    request.setAttribute("isAuditFlag", "0");
                }
                request.setAttribute("auditTabFlag", auditTabFlag);
            }
        }
        return "bpa/lay-InternalContractReview";
    }

    @RequestMapping("/auditProcess")
    @ResponseBody
    public RspVo auditProcess(String id, String processId, String status, String auditMsg) {
        //审批
        bpaProcessManageService.auditProject(id, processId, status, auditMsg);
        return RspVo.getSuccessResponse();
    }
//    /**
//     * 内部协议-我审批的列表数据
//     *
//     * @return
//     */
//    @RequestMapping("/queryMyCheckDataByCondition")
//    @ResponseBody
//    public RspVo<InnerAgrtDomain> queryMyCheckDataByCondition(InnerAgrtVo innerAgrtVo,
//                                                              ParamPage paramPage) {
//        Page<InnerAgrtDomain> page = new Page<InnerAgrtDomain>();
//        page.setPageNum(paramPage.getPage());
//        page.setPageSize(paramPage.getLimit());
//        //查询时，结束日期增加一天
//        if (innerAgrtVo.getEndDate() != null) {
//            innerAgrtVo.setEndDate(DateUtils.addDays(innerAgrtVo.getEndDate(), 1));
//        }
//        innerAgrtVo.setAddBy(SessionUserUtils.getSessionUserId());
//        page = innerAgrtService.selectMyCheckByDomainWhere(page, innerAgrtVo);
//        return RspVo.getSuccessResponse(page.getTotal(), page.getResult());
//    }

//    /**
//     * 内部协议-新增
//     * @return
//     */
//    @RequestMapping("/add")
//    public String toAdd(HttpServletRequest request) {
//        String bindOrgId = String.valueOf(SessionUserUtils.getSessionAttribute("bindOrgId"));
//        String bindOrgName = String.valueOf(SessionUserUtils.getSessionAttribute("bindOrgName"));
//        String bindDeptId = String.valueOf(SessionUserUtils.getSessionAttribute("bindDeptId"));
//        String bindDeptName = String.valueOf(SessionUserUtils.getSessionAttribute("bindDeptName"));
//        request.setAttribute("bindOrgId", bindOrgId);
//        request.setAttribute("bindOrgName", bindOrgName);
//        request.setAttribute("bindDeptId", bindDeptId);
//        request.setAttribute("bindDeptName", bindDeptName);
//        AuthorizationVO authorizationVO = SessionUserUtils.getSessionAttributeForUserDtl();
//        StaffInfo staffInfo = staffInfoService.selectByPrimaryKey(authorizationVO.getHrStaffId());
//        request.setAttribute("staffName", staffInfo.getStaffName());
//        //接收前端参数 判断 原合同  和  补充协议
//        String types = request.getParameter("types");
//        if ("1".equals(types)) {
//            //返回所有承继协议中的项目名称
//            List<ContractExt> contractExtList = contractExtService.selectAllUsableContractExt(
//                null, null);
//            request.setAttribute("contractExtList", contractExtList);
//            //返回所有合作单位为总包单位的信息
//            List<CoopInfo> coopInfoList = coopInfoService.selectAll("3", null, null);
//            request.setAttribute("coopInfoList", coopInfoList);
//            return "bpa/lay-innerAgrt-add-1";
//        }
//        else if ("2".equals(types)) {
//            //返回内部协议中的项目名称
//            List<InnerAgrt> InnerAgrtList = innerAgrtService.selectInnerAgrt();
//            request.setAttribute("InnerAgrtList", InnerAgrtList);
//            return "bpa/lay-innerAgrt-add-2";
//        }
//        return null;
//    }

//    /**
//     * 内部协议-新增
//     */
//    @SuppressWarnings("rawtypes")
//    @RequestMapping("/save")
//    @ResponseBody
//    public RspVo save(InnerAgrtAssist innerAgrtAssist) {
//
//        String proCode = "";
//        String[] strCodes = innerAgrtAssist.getProCodeAndCodes().split("\\_");
//        proCode = strCodes[0];
//        //主表-内部协议
//        innerAgrtAssist.setHrDeptId(innerAgrtAssist.getHrDeptid());
//        innerAgrtAssist.setHrOrgId(innerAgrtAssist.getHrOrgid());
//        innerAgrtAssist.setStaffName(innerAgrtAssist.getStaffname());
//        innerAgrtAssist.setHrStaffApplyId(innerAgrtAssist.getHrStaffApplyid());
//        String code = "";
//        code = strCodes[1];
//        innerAgrtAssist.setProCode(proCode);
//        String[] sgCoopIds = innerAgrtAssist.getSgCoopIds().split("\\_");
//        innerAgrtAssist.setSgCoopId(sgCoopIds[0]);
//        AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
//        String id = IdFactory.getUUIDUpper();
//        //添加项目名称
//        innerAgrtAssist.setProName(strCodes[2]);
//        //父关联id  第一次添加时为  原合同
//        innerAgrtAssist.setRefId(code);
//        innerAgrtAssist.setId(id);
//        innerAgrtAssist.setAddBy(vo.getId());
//        innerAgrtAssist.setAddTime(top.infopub.mgr.util.DateUtils.getCurrentDateTime());
//        String codes = codeGeneratorConfigService.generatorCode("bpa_inner_agrt");
//        innerAgrtAssist.setCodes(codes);
//        //合同类型  先设置成  原合同  如果是补充协议的话  就会在 下面if中 形成覆盖
//        innerAgrtAssist.setExtType("1");
//        //合同版本 先设置成   原合同  每次提交时都进行 累加
//        innerAgrtAssist.setVersionNo(1);
//        // 处理processId
//        innerAgrtAssist.setProcessId(IdFactory.getUUIDUpper());
//        innerAgrtAssist.setProcessStatus("1");//审批中
//        innerAgrtAssist.setTypes("1");//PPP项目
//        innerAgrtAssist.setUptBy(vo.getId());
//        innerAgrtAssist.setUptTime(top.infopub.mgr.util.DateUtils.getCurrentDateTime());
//
//        //内部协议对应月度付款比例
//        List<InnerAgrtRate> innerAgrtRates = new ArrayList<InnerAgrtRate>();
//        //序号
//        String[] showIdxs = innerAgrtAssist.getShowIdxs();
//        if (showIdxs != null && showIdxs.length > 0) {
//            for (int i = 0; i < showIdxs.length; i++ ) {
//                String showIdx = showIdxs[i];
//                String minValue = innerAgrtAssist.getMinValues()[i];
//                String maxValue = innerAgrtAssist.getMaxValues()[i];
//                String payRate = innerAgrtAssist.getPayRates()[i];
//                if (StringUtils.isEmpty(showIdx) && StringUtils.isEmpty(showIdx)) {
//                    continue;
//                }
//                InnerAgrtRate innerAgrtRate = new InnerAgrtRate();
//                String innerAgrtRateId = IdFactory.getUUIDUpper();//自动生成主键
//                innerAgrtRate.setId(innerAgrtRateId);
//                innerAgrtRate.setInnerAgrtId(id);
//                innerAgrtRate.setShowIdx(Integer.parseInt(showIdx));
//                innerAgrtRate.setMaxValue(new BigDecimal(maxValue));
//                innerAgrtRate.setMinValue(new BigDecimal(minValue));
//                innerAgrtRate.setPayRate(new BigDecimal(payRate));
//                innerAgrtRate.setAddBy(vo.getId());
//                innerAgrtRate.setAddTime(top.infopub.mgr.util.DateUtils.getCurrentDateTime());
//                innerAgrtRates.add(innerAgrtRate);
//            }
//        }
//
//        //新增内部协议清单分解子项信息
//        List<InnerAgrtSub> innerAgrtSubs = new ArrayList<InnerAgrtSub>();
//        //内部协议清单分解子项费用明细
//        List<InnerAgrtDetail> innerAgrtDetails = new ArrayList<InnerAgrtDetail>();
//        if ("2".equals(innerAgrtAssist.getJudge())) {
//            //判断补充协议名称是否存在
//            List<InnerAgrt> innerAgrtSaName = innerAgrtService.isExistInnerAgrtSaName(
//                innerAgrtAssist.getInnerAgrtSaName(), proCode);
//            if (CollectionUtils.isNotEmpty(innerAgrtSaName)) {
//                return ErrorCodeEnum.ERROR_COMMON_SAVE.getRspVo("补充协议名称【"
//                                                                + innerAgrtAssist.getInnerAgrtSaName()
//                                                                + "】已经存在~");
//            }
//            //合同类型  覆盖上面的合同类型
//            innerAgrtAssist.setExtType("2");
//            //合同版本  每次提交时都进行 累加
//            InnerAgrt innerAgrt = new InnerAgrt();
//            innerAgrt.setRefId(code);
//            List<InnerAgrt> entityWhere = innerAgrtService.selectByEntityWhere(innerAgrt);
//            Integer versionNo = entityWhere.size() + 2;
//            innerAgrtAssist.setVersionNo(versionNo);
//            //父关联id  添加时为  补充协议合同  原合同id
//            innerAgrtAssist.setRefId(code);
//        }
//        else {
//            /*List<InnerAgrt> existProName = innerAgrtService.isExistProName(proCode);
//            if (CollectionUtils.isNotEmpty(existProName)) {
//                return ErrorCodeEnum.ERROR_COMMON_SAVE.getRspVo("项目名称【" + innerAgrtAssist.getProName()
//                                                                + "】已经存在~");
//            }*/
//            //判断内部协议名称是否存在
//            List<InnerAgrt> innerAgrtName = innerAgrtService.isExistInnerAgrtName(
//                innerAgrtAssist.getInnerAgrtName(), proCode);
//            if (CollectionUtils.isNotEmpty(innerAgrtName)) {
//                return ErrorCodeEnum.ERROR_COMMON_SAVE.getRspVo("内部协议名称【"
//                                                                + innerAgrtAssist.getInnerAgrtName()
//                                                                + "】已经存在~");
//            }
//        }
//
//        //子项名称
//        String[] subNames = innerAgrtAssist.getSubNames();
//        if (subNames != null && subNames.length > 0) {
//            for (int i = 0; i < subNames.length; i++ ) {
//                String subName = subNames[i];
//                if (StringUtils.isEmpty(subName)) {
//                    continue;
//                }
//                InnerAgrtSub innerAgrtSub = new InnerAgrtSub();
//                String innerAgrtSubId = IdFactory.getUUIDUpper();//自动生成主键
//                innerAgrtSub.setId(innerAgrtSubId);
//                innerAgrtSub.setInnerAgrtId(id);
//                innerAgrtSub.setSubName(subName);
//                innerAgrtSub.setAddBy(vo.getId());
//                Date addTime = top.infopub.mgr.util.DateUtils.addSeconds(
//                    top.infopub.mgr.util.DateUtils.getCurrentDateTime(), i);
//                innerAgrtSub.setAddTime(addTime);
//                innerAgrtSubs.add(innerAgrtSub);
//            }
//
//            //费用大类
//            String[] feeTypes = innerAgrtAssist.getFeeTypes();
//            for (int j = 0; j < feeTypes.length; j++ ) {
//                String feeType = feeTypes[j];
//                String feeSmallType = innerAgrtAssist.getFeeSmallTypes()[j];
//                String itemNameSub = innerAgrtAssist.getItemNameSub()[j];
//                String typeName = innerAgrtAssist.getTypeNames()[j];
//                String orderIdx = innerAgrtAssist.getOrderIdxs()[j];
//                String innerDownRate = innerAgrtAssist.getInnerDownRates()[j];
//                String outDownRate = innerAgrtAssist.getOutDownRates()[j];
//                String afterDownFee = innerAgrtAssist.getAfterDownFees()[j];
//                String proContractPreFee = innerAgrtAssist.getProContractPreFees()[j];
//                String packPreFee = innerAgrtAssist.getPackPreFees()[j];
//                if (StringUtils.isEmpty(feeType)) {
//                    continue;
//                }
//                InnerAgrtDetail innerAgrtDetail = new InnerAgrtDetail();
//                String innerAgrtDetailId = IdFactory.getUUIDUpper();//自动生成主键
//                innerAgrtDetail.setId(innerAgrtDetailId);
//
//                innerAgrtDetail.setAgrtSubId(itemNameSub);
//
//                innerAgrtDetail.setInnerAgrtId(id);
//                innerAgrtDetail.setFeeType(1);
//                innerAgrtDetail.setOrderIdx(orderIdx);
//                innerAgrtDetail.setTypeName(typeName);
//                if ("-1".equals(feeSmallType)) {
//                    innerAgrtDetail.setFeeSmallType(Integer.parseInt(feeSmallType));
//                }
//                else {
//                    String feeSmallType1 = feeSmallType.charAt(0) + "" + feeSmallType.charAt(2);
//                    innerAgrtDetail.setFeeSmallType(Integer.parseInt(feeSmallType1));
//                }
//                innerAgrtDetail.setProContractPreFee(new BigDecimal(proContractPreFee));
//                innerAgrtDetail.setPackPreFee(new BigDecimal(packPreFee));
//                innerAgrtDetail.setOutDownRate(new BigDecimal(outDownRate));
//                innerAgrtDetail.setAfterDownFee(new BigDecimal(afterDownFee));
//                innerAgrtDetail.setInnerDownRate(new BigDecimal(innerDownRate));
//                innerAgrtDetail.setAddBy(vo.getId());
//                innerAgrtDetail.setAddTime(top.infopub.mgr.util.DateUtils.getCurrentDateTime());
//                innerAgrtDetails.add(innerAgrtDetail);
//            }
//
//            for (InnerAgrtDetail innerAgrtDetail : innerAgrtDetails) {
//                for (InnerAgrtSub innerAgrtSub : innerAgrtSubs) {
//                    if (innerAgrtDetail.getAgrtSubId().equals(innerAgrtSub.getSubName())) {
//                        innerAgrtDetail.setAgrtSubId(innerAgrtSub.getId());
//                        break;
//                    }
//                }
//            }
//
//        }
//
//        //上传附件
//        //1=Word版合同文本
//        List<InnerAgrtAtt> innerAgrtAtts = new ArrayList<InnerAgrtAtt>();
//        String[] fileAtt1 = innerAgrtAssist.getFileAtts1();
//        if (fileAtt1 != null) {
//            for (String file : fileAtt1) {
//                InnerAgrtAtt innerAgrtAtt = createFileAtt(vo, id, file,
//                    innerAgrtAssist.getExtType());
//                innerAgrtAtt.setTypes("1");
//                innerAgrtAtts.add(innerAgrtAtt);
//            }
//        }
//        //2=PDF版合同文本
//        String[] fileAtt2 = innerAgrtAssist.getFileAtts2();
//        if (fileAtt2 != null) {
//            for (String file : fileAtt2) {
//                InnerAgrtAtt innerAgrtAtt = createFileAtt(vo, id, file,
//                    innerAgrtAssist.getExtType());
//                innerAgrtAtt.setTypes("2");
//                innerAgrtAtts.add(innerAgrtAtt);
//            }
//        }
//        //3=盖章版合同文本（选填）
//        String[] fileAtt3 = innerAgrtAssist.getFileAtts3();
//        if (fileAtt3 != null) {
//            for (String file : fileAtt3) {
//                InnerAgrtAtt innerAgrtAtt = createFileAtt(vo, id, file,
//                    innerAgrtAssist.getExtType());
//                innerAgrtAtt.setTypes("3");
//                innerAgrtAtts.add(innerAgrtAtt);
//            }
//        }
//        //4=其他（选填）
//        String[] fileAtt4 = innerAgrtAssist.getFileAtts4();
//        if (fileAtt4 != null) {
//            for (String file : fileAtt4) {
//                InnerAgrtAtt innerAgrtAtt = createFileAtt(vo, id, file,
//                    innerAgrtAssist.getExtType());
//                innerAgrtAtt.setTypes("4");
//                innerAgrtAtts.add(innerAgrtAtt);
//            }
//        }
//        innerAgrtService.saveInnerAgrt(innerAgrtAssist, innerAgrtSubs, innerAgrtDetails,
//            innerAgrtRates, innerAgrtAtts);
//        LogUtil.bizLog(LogModelType.PROJECT, AcctionType.ADD, "商务管理 -对内合同-内部协议：项目名="
//                                                              + innerAgrtAssist.getProName(), null);
//        return RspVo.getSuccessResponse();
//    }
//
//    /**
//     * 内部协议-补充签订
//     * @throws ParseException
//     */
//    @SuppressWarnings("rawtypes")
//    @RequestMapping("/sign")
//    @ResponseBody
//    public RspVo sign(HttpServletRequest request, InnerAgrtVo innerAgrtVo)
//        throws ParseException {
//        String[] fileAtt1 = request.getParameterValues("fileAtt1");
//        String[] fileAtt2 = request.getParameterValues("fileAtt2");
//        String[] fileAtt3 = request.getParameterValues("fileAtt3");
//        String[] fileAtt4 = request.getParameterValues("fileAtt4");
//
//        AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
//        String id = innerAgrtVo.getId();
//        List<InnerAgrtAtt> innerAgrtAtts = new ArrayList<InnerAgrtAtt>();
//        if (fileAtt1 != null) {
//            for (String file : fileAtt1) {
//                InnerAgrtAtt innerAgrtAtt = createFileAtt(vo, id, file, "1");
//                innerAgrtAtt.setTypes("1");
//                innerAgrtAtts.add(innerAgrtAtt);
//            }
//        }
//        if (fileAtt2 != null) {
//            for (String file : fileAtt2) {
//                InnerAgrtAtt innerAgrtAtt = createFileAtt(vo, id, file, "1");
//                innerAgrtAtt.setTypes("2");
//                innerAgrtAtts.add(innerAgrtAtt);
//            }
//        }
//        if (fileAtt3 != null) {
//            for (String file : fileAtt3) {
//                InnerAgrtAtt innerAgrtAtt = createFileAtt(vo, id, file, "1");
//                innerAgrtAtt.setTypes("3");
//                innerAgrtAtts.add(innerAgrtAtt);
//            }
//        }
//        if (fileAtt4 != null) {
//            for (String file : fileAtt4) {
//                InnerAgrtAtt innerAgrtAtt = createFileAtt(vo, id, file, "1");
//                innerAgrtAtt.setTypes("4");
//                innerAgrtAtts.add(innerAgrtAtt);
//            }
//        }
//        innerAgrtService.sign(innerAgrtVo, innerAgrtAtts);
//        LogUtil.bizLog(LogUtil.LogModelType.PROJECT, LogUtil.AcctionType.ADD,
//            "商务管理 -对内合同-补充签订内部协议：项目名=" + innerAgrtVo.getProName(), null);
//        return RspVo.getSuccessResponse();
//    }
//
//    private InnerAgrtAtt createFileAtt(AuthorizationVO vo, String innerAgrtId, String file,
//                                       String extType) {
//        //资料信息数组
//        String[] fileAttStr = file.split("\\|");
//        InnerAgrtAtt fileAtt = new InnerAgrtAtt();
//        if (extType.equals("1") && StringUtils.isNotEmpty(fileAttStr[2])) {
//            fileAtt.setId(fileAttStr[2]);
//        }
//        else {
//            fileAtt.setId(IdFactory.getUUIDUpper());
//        }
//        fileAtt.setInnerAgrtId(innerAgrtId);//项目id
//        String fileName = fileAttStr[1].substring(0, fileAttStr[1].lastIndexOf("."));
//        String suffix = fileAttStr[1].substring(fileAttStr[1].lastIndexOf("."),
//            fileAttStr[1].length()).toLowerCase();//后缀转小写
//        fileAtt.setFileName(fileName);//文件名
//        fileAtt.setFileSuffix(suffix);//文件后缀
//        fileAtt.setFilePath(fileAttStr[3]);//文件保存路径
//        fileAtt.setAddBy(vo.getId());//添加人
//        fileAtt.setAddTime(top.infopub.mgr.util.DateUtils.getCurrentDateTime());//添加时间
//        return fileAtt;
//    }
//
//    /**
//     * 上传附件
//     */
//    @SuppressWarnings("rawtypes")
//    @RequestMapping("/uploadAtt")
//    @ResponseBody
//    public RspVo updateAtt(HttpServletRequest request) {
//        List<String> returnFileStrList = new ArrayList<>();
//        MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest)request;
//        Map<String, MultipartFile> fileMap = multipartHttpServletRequest.getFileMap();
//        if (fileMap != null) {
//            for (String name : fileMap.keySet()) {
//                MultipartFile file = fileMap.get(name);
//                //资料类型
//                String types = request.getParameter("types");
//                if (StringUtils.isEmpty(types)) {
//                    return ErrorCodeEnum.ERROR_COMMON_PARAM.getRspVo("缺少参数");
//                }
//                //上传文件
//                String fileId = IdFactory.getUUIDSerialNumber();
//                //服务器资源路径
//                String serverPath = "innerAgrt";
//                String filePath = FileHelper.upload(file, serverPath);
//                returnFileStrList.add("|" + file.getOriginalFilename().trim() + "|" + fileId + "|"
//                                      + filePath);
//                LogUtil.bizLog(LogModelType.PROJECT, AcctionType.ADD, "上传对内合同-内部协议附件-文件名:"
//                                                                      + filePath + "，文件路径:"
//                                                                      + filePath, null);
//            }
//        }
//        return RspVo.getSuccessResponse(returnFileStrList);
//    }

//    /**
//     * 根据承继协议中项目编号查询相关带入信息(内部协议/原合同)
//     * @param request
//     * @return
//     * @throws Exception
//     */
//    @RequestMapping("/getContractExtByProCode")
//    @ResponseBody
//    public Map<String, Object> getContractExtByProCode(HttpServletRequest request) {
//        boolean flag = true;
//        Map<String, Object> map = new HashMap<String, Object>();
//        String proCode = request.getParameter("proCodeAndCodes");
//        String[] split = proCode.split("\\_");
//        String sgCoopId = request.getParameter("sgCoopIds");
//        try {
//            //带出内部协议的信息
//            Map<String, Object> mapWhere = new HashMap<String, Object>();
//            ContractExtDomain contractExtDomain = new ContractExtDomain();
//            mapWhere.put("refId", split[1]);
//            mapWhere.put("processStatus", "9");
//            mapWhere.put("orderCondition", "ADD_TIME desc");
//            List<ContractExt> contractExts = contractExtService.selectByMapWhere(mapWhere);
//            if (CollectionUtils.isNotEmpty(contractExts)) {
//                contractExtDomain = contractExtService.selectByIds(contractExts.get(0).getId());
//            }
//            else {
//                contractExtDomain = contractExtService.selectByIds(split[1]);
//            }
//            if (StringUtils.isNotEmpty(sgCoopId) && contractExtDomain != null) {
//                String[] sgCoopName = sgCoopId.split("\\_");
//                contractExtDomain.setCoopName(sgCoopName[1]);
//            }
//            //带出 合同清单子项  和  合同清单子项费用明细  信息
//            if (contractExtDomain != null) {
//                List<ContractExtDetailAndSubDomain> selectDetailAndSubList = contractExtDetailService.selectDetailAndSub(
//                    contractExtDomain.getId(), "1", null, null, null,null);
//                if(CollectionUtils.isNotEmpty(selectDetailAndSubList)){
//                    for (ContractExtDetailAndSubDomain cSubDomain : selectDetailAndSubList) {
//                        if (cSubDomain.getFeeSmallType() == -1) {
//                            BigDecimal detailAmount = contractExtDetailService.selectByDetailAmount(cSubDomain.getExtSubId());
//                            cSubDomain.setOutPreDownAmountDetail(detailAmount);
//                        }
//                    }
//                }
//                BigDecimal detailAmountSum = contractExtDetailService.selectByDetailAmountSum(contractExtDomain.getId());
//                map.put("detailAmountSum", detailAmountSum);
//                ContractExtInvest contractExtInvest = new ContractExtInvest();
//                contractExtInvest.setContractExtId(contractExtDomain.getId());
//                List<ContractExtInvest> contractExtInvests = contractExtInvestService.selectByEntityWhere(contractExtInvest);
//                map.put("contractExtInvest", contractExtInvests.get(0));
//                map.put("contractExtProCode", contractExtDomain);
//                map.put("selectDetailAndSubList", selectDetailAndSubList);
//            }
//        }
//        catch (Exception e) {
//            log.error("查询承继协议记录异常", e);
//            flag = false;
//        }
//        map.put("flag", flag);
//        return map;
//    }

//    /**
//     * 根据内部协议中项目编号查询相关带入信息(补充协议)
//     * @param request
//     * @return
//     * @throws Exception
//     */
//    @RequestMapping("/getInnerAgrtByProCode")
//    @ResponseBody
//    public Map<String, Object> getInnerAgrtByProCode(HttpServletRequest request) {
//        boolean flag = true;
//        Map<String, Object> map = new HashMap<String, Object>();
//        String proCode = request.getParameter("proCodeAndCodes");
//        String[] split = proCode.split("\\_");
//        try {
//            Map<String, Object> mapWhere = new HashMap<String, Object>();
//            InnerAgrtDomain innerAgrtDomain = new InnerAgrtDomain();
//            mapWhere.put("refId", split[1]);
//            mapWhere.put("processStatus", "9");
//            mapWhere.put("orderCondition", "ADD_TIME desc");
//            List<InnerAgrt> innerAgrts = innerAgrtService.selectByMapWhere(mapWhere);
//            if (CollectionUtils.isNotEmpty(innerAgrts)) {
//                innerAgrtDomain = innerAgrtService.selectByIds(innerAgrts.get(0).getId());
//            }
//            else {
//                innerAgrtDomain = innerAgrtService.selectByIds(split[1]);
//            }
//            //InnerAgrtDomain innerAgrtProCode = innerAgrtService.selectByInnerAgrtProCode(split[0],
//            // null);
//            if (innerAgrtDomain != null) {
//                map.put("innerAgrtProCode", innerAgrtDomain);
//                //综合计划完成率对应月度付款比例
//                List<InnerAgrtRate> innerAgrtRateList = innerAgrtRateService.selectInnerAgrtRateByInnerAgrtId(innerAgrtDomain.getId());
//                map.put("innerAgrtRateList", innerAgrtRateList);
//                //附件
//                List<InnerAgrtAtt> innerAgrtAttList = innerAgrtAttService.selectInnerAgrtAttByInnerAgrtId(innerAgrtDomain.getId());
//                map.put("innerAgrtAttList", innerAgrtAttList);
//                //合同价款清单
//                List<InnerAgrtDetailAndSubDomain> byInnerAgrtIdList = innerAgrtDetailService.selectByInnerAgrtId(innerAgrtDomain.getId());
//                map.put("innerAgrtDetailAndSubList", byInnerAgrtIdList);
//            }
//        }
//        catch (Exception e) {
//            log.error("查询内部协议记录异常", e);
//            flag = false;
//        }
//        map.put("flag", flag);
//        return map;
//    }
//
//    /**
//     * 查询是否显示补充签订
//     * @param request
//     * @return
//     * @throws Exception
//     */
//    @RequestMapping("/supply")
//    @ResponseBody
//    public Map<String, Object> supply() {
//        boolean flag = true;
//        Map<String, Object> map = new HashMap<String, Object>();
//        try {
//            InnerAgrt innerAgrt = innerAgrtService.selectInnerAgrtSignDate();
//            map.put("innerAgrt", innerAgrt);
//        }
//        catch (Exception e) {
//            log.error("查询记录异常", e);
//            flag = false;
//        }
//        map.put("flag", flag);
//        return map;
//    }
//
//    /**
//     * 返回合同信息中的 居间费承担方  农民工保证金承担方  履约保函承担方
//     * @param request
//     * @return
//     * @throws Exception
//     */
//    @RequestMapping("/getCoopInfoByCoopId")
//    @ResponseBody
//    public Map<String, Object> getCoopInfoByCoopId(HttpServletRequest request) {
//        boolean flag = true;
//        Map<String, Object> map = new HashMap<String, Object>();
//        InnerAgrtAssist innerAgrt = new InnerAgrtAssist();
//        try {
//            String proCodeAndCodes = request.getParameter("proCodeAndCodes");
//            String sgCoopIds = request.getParameter("sgCoopIds");
//            if (StringUtils.isNotEmpty(sgCoopIds)) {
//                String[] sgCoopId = sgCoopIds.split("\\_");
//                innerAgrt.setCoopNames(sgCoopId[1]);
//            }
//            if (StringUtils.isNotEmpty(proCodeAndCodes)) {
//                String[] proCodeAndCode = proCodeAndCodes.split("\\_");
//                innerAgrt.setProNames(proCodeAndCode[2]);
//            }
//            map.put("innerAgrt", innerAgrt);
//        }
//        catch (Exception e) {
//            log.error("查询记录异常", e);
//            flag = false;
//        }
//        map.put("flag", flag);
//        return map;
//    }
//
//    /**
//     * 根据id查询设备合同详细  > 注意：方法命名必须为：toDealOrDetail
//     * @param id 分包合同id
//     * @return
//     * @see
//     */
//    @RequestMapping("/toDealOrDetail")
//    public ModelAndView queryById(HttpServletRequest request, String id, String processDealWith) {
//        Map<String, Object> map = new HashMap<String, Object>();
//        InnerAgrtVo innerAgrtVo = innerAgrtService.queryByIds(id);
//        OrgInfo orgInfo = new OrgInfo();
//        orgInfo.setProjectCode(innerAgrtVo.getProCode());
//        orgInfo.setState(1);
//        List<OrgInfo> orgInfos = orgInfoService.selectByEntityWhere(orgInfo);
//        if (orgInfos.size() > 0) {
//            innerAgrtVo.setOrgProName(orgInfos.get(0).getCompName());
//            innerAgrtVo.setHrOrgProId(orgInfos.get(0).getId());
//            OrgInfo parentOrgInfo = new OrgInfo();
//            parentOrgInfo.setId(orgInfos.get(0).getParentId());
//            parentOrgInfo.setState(1);
//            List<OrgInfo> parentOInfos = orgInfoService.selectByEntityWhere(parentOrgInfo);
//            if (parentOInfos.size() > 0) {
//                innerAgrtVo.setOrgAreaName(parentOInfos.get(0).getCompName());
//                innerAgrtVo.setHrOrgAreaId(parentOInfos.get(0).getId());
//            }
//        }
//        //原总包合同范围内金额(下浮前)(合计)
//        BigDecimal sumPreScopeFee = subpackCotService.sumPreScopeFee(innerAgrtVo.getProCode());
//        //原总包合同范围内金额(下浮后)(合计)
//        BigDecimal sumAfterScopeFee = subpackCotService.sumAfterScopeFee(innerAgrtVo.getProCode());
//        //扣减二次分包合同总价(元)(下浮前)
//        BigDecimal preScopeFees = innerAgrtVo.getPreTotalSum().subtract(
//            sumPreScopeFee == null ? BigDecimal.ZERO : sumPreScopeFee);
//        //扣减二次分包合同总价(元)(下浮后)
//        BigDecimal afterScopeFees = innerAgrtVo.getAfterTotalSum().subtract(
//            sumAfterScopeFee == null ? BigDecimal.ZERO : sumAfterScopeFee);
//        innerAgrtVo.setPreScopeFees(preScopeFees);
//        innerAgrtVo.setAfterScopeFees(afterScopeFees);
//        //扣减整体下浮率
//        BigDecimal scopeFees = innerAgrtVo.getScopeFees();
//        BigDecimal sum = BigDecimal.ONE;
//        if (sumPreScopeFee == null || sumAfterScopeFee == null
//            || sumPreScopeFee == BigDecimal.ZERO || sumAfterScopeFee == BigDecimal.ZERO) {
//            scopeFees = BigDecimal.ONE;
//        }
//        else if (sumPreScopeFee != null && sumAfterScopeFee != null
//                 && sumPreScopeFee != BigDecimal.ZERO && sumAfterScopeFee != BigDecimal.ZERO) {
//            scopeFees = sum.subtract(sumAfterScopeFee.divide(sumPreScopeFee, 2,
//                BigDecimal.ROUND_HALF_UP));
//        }
//        innerAgrtVo.setScopeFees(scopeFees);
//        request.setAttribute("modelDetail", innerAgrtVo);
//        map.put("innerAgrtVo", innerAgrtVo);
//        // 审批flag
//        request.setAttribute("processDealWith", processDealWith);
//        // 审批data
//        processData(request, innerAgrtVo.getProcessId());
//        return new ModelAndView("bpa/lay-innerAgrt-detail", map);
//    }
//    /**
//     * 内部协议原合同/补充合同-复制单据
//     * @return
//     */
//    @RequestMapping("/toCopy")
//    public String toCopy(HttpServletRequest request) {
//        String id = request.getParameter("id");
//        String extType = request.getParameter("extType");
//        InnerAgrtVo innerAgrtVo = innerAgrtService.queryByIds(id);
//        request.setAttribute("innerAgrt",innerAgrtVo);
//        //返回所有合作单位为总包单位的信息
//        List<CoopInfo> coopInfoList = coopInfoService.selectAll("3", null, null);
//        request.setAttribute("coopInfoList", coopInfoList);
//
//        if ("1".equals(extType)) {
//            return "bpa/lay-innerAgrt-copy-1";
//        }
//        else if ("2".equals(extType)) {
//            return "bpa/lay-innerAgrt-copy-2";
//        }
//        return null;
//    }
//    /**
//     * 根据内部协议id查询相关带入信息
//     * @param request
//     * @return
//     * @throws Exception
//     */
//    @RequestMapping("/getInnerAgrtById")
//    @ResponseBody
//    public Map<String, Object> getInnerAgrtById(HttpServletRequest request) {
//        boolean flag = true;
//        Map<String, Object> map = new HashMap<String, Object>();
//        String proCodeAndCodes = request.getParameter("proCodeAndCodes");
//        String[] strCodes = proCodeAndCodes.split("\\_");
//        try {
//            InnerAgrtVo innerAgrtVo = innerAgrtService.queryByIds(strCodes[1]);
//            map.put("innerAgrtVo", innerAgrtVo);
//        }catch(Exception e) {
//            log.error("查询内部协议记录异常", e);
//            flag = false;
//        }
//        map.put("flag", flag);
//        return map;
//    }
}
